package com.android.yinghebike.ui.activity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.yinghebike.BuildConfig;
import com.android.yinghebike.R;
import com.android.yinghebike.constants.Constants;
import com.android.yinghebike.manager.API_Manager;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.security.Permissions;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PermissionSettingActivity extends BaseActivity {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.tv_tongzhi)
    TextView tv_tongzhi;
    @BindView(R.id.tv_location)
    TextView tv_location;
    @BindView(R.id.tv_net)
    TextView tv_net;
    @BindView(R.id.tv_bluetooth)
    TextView tv_bluetooth;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_permission_setting;
    }

    @Override
    protected void initView() {
        comm_title.setTitle(getString(R.string.mine_permission));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        checkPermission();
    }

    private void checkPermission() {
        setText(tv_tongzhi, Permission.NOTIFICATION_SERVICE);
        setText(tv_location, Permission.ACCESS_FINE_LOCATION);
        tv_net.setText(getString(R.string.permission_opened));
        tv_net.setTextColor(Color.parseColor("#ffffff"));

        List<String> blueToothNeedPermissions = getBlueToothNeedPermissions();
        setText(tv_bluetooth,   blueToothNeedPermissions.toArray(new String[blueToothNeedPermissions.size()]));

    }

    private void setText(TextView textView, String... permision) {
        if (XXPermissions.isGranted(mContext, permision)) {
            textView.setText(getString(R.string.permission_opened));
            textView.setTextColor(Color.parseColor("#ffffff"));
        } else {
            textView.setText(getString(R.string.permission_unopen));
            textView.setTextColor(Color.parseColor("#FB5A50"));
        }
    }

//    private void setText(TextView textView, String[]... permision) {
//        if (XXPermissions.isGranted(mContext, permision)) {
//            textView.setText("已开启");
//            textView.setTextColor(Color.parseColor("#ffffff"));
//        } else {
//            textView.setText("未开启");
//            textView.setTextColor(Color.parseColor("#FB5A50"));
//        }
//    }


    @OnClick({R.id.ll_tongzhi,R.id.ll_location,R.id.ll_bluetooth })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_tongzhi:
                XXPermissions.startPermissionActivity(mContext, Permission.NOTIFICATION_SERVICE);
                break;
            case R.id.ll_location:
                XXPermissions.startPermissionActivity(mContext, Permission.ACCESS_FINE_LOCATION);
                break;
            case R.id.ll_bluetooth:
                XXPermissions.startPermissionActivity(mContext, Permission.Group.BLUETOOTH);
                break;
        }
    }

    //需要进行检测的权限
    private List<String> getBlueToothNeedPermissions() {
        List<String> list = new ArrayList<>();
        //target sdk版本在29以上的需要精确定位权限才能搜索到蓝牙设备
        if (getApplicationInfo().targetSdkVersion >= 29) {
            list.add(Manifest.permission.ACCESS_FINE_LOCATION);
            list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        } else {
            list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        //Android 12需要
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            list.add(Manifest.permission.BLUETOOTH_SCAN);
            list.add(Manifest.permission.BLUETOOTH_CONNECT);
            list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }
        return list;
    }
}
